package com.mz.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mz.common.dto.GlobalException;
import com.mz.common.dto.PaginationVo;
import com.mz.dao.SmsFeedbackDao;
import com.mz.entity.SmsBanner;
import com.mz.entity.SmsFeedback;
import com.mz.entity.vo.FeedbackVo;
import com.mz.service.SmsFeedbackService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class SmsFeedbackServiceImpl implements SmsFeedbackService {

    @Resource
    private SmsFeedbackDao smsFeedbackDao;


    @Override
    public IPage getFeedbackpPaginatin(PaginationVo paginationVo) throws Exception {

        IPage page = new Page( paginationVo.getPageIndex(), paginationVo.getPageSize() );
        LambdaQueryWrapper<SmsFeedback> lqs = new LambdaQueryWrapper<>();
        lqs.orderByDesc(SmsFeedback::getCreateTime);

        smsFeedbackDao.selectPage(page, lqs);
        return page;
    }

    @Override
    @Transactional
    public void addFeedback(FeedbackVo feedbackVo) throws Exception {

        if( feedbackVo.getId() != null ){
            throw GlobalException.gen(10031, "用户反馈更新接口对接错误");
        }
        SmsFeedback smsFeedback = new SmsFeedback();
        smsFeedback.setPhone( feedbackVo.getPhone())
                .setContent(feedbackVo.getContent());

        StringBuilder urlStringBuilder = new StringBuilder();
        if( feedbackVo.getUrlList() != null ){
            for( String url : feedbackVo.getUrlList() ){
                urlStringBuilder.append(url);
                urlStringBuilder.append("#####");
            }
            smsFeedback.setContent( urlStringBuilder.toString().substring(0, urlStringBuilder.length()-5) );
        }
        smsFeedbackDao.insert(smsFeedback);
    }


    @Override
    @Transactional
    public void removeFeedback(List<Long> idList) throws Exception {
        if( idList == null || idList.size() == 0 ){
            throw GlobalException.gen(10014, "用户反馈id不存在");
        }
        smsFeedbackDao.deleteBatchIds(idList);
        return;
    }
}
